package com.yxzr.youyou.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.yxzr.youyou.R;
import com.yxzr.youyou.VideoDetailActivity;
import com.yxzr.youyou.bean.MyCollectBean;
import com.yxzr.youyou.retrofit.DataUrl;

import java.util.ArrayList;
import java.util.List;

public class MyMovieAdapter extends RecyclerView.Adapter<MyMovieAdapter.ViewHolder> {
    private Context mContext;
    private List<MyCollectBean.DataBean> list = new ArrayList<>();
    private int indexVis = 0;
    private OnClickListener onClickListener;
    private SparseBooleanArray mSelectedPositions = new SparseBooleanArray();
    public MyMovieAdapter(Context context){
        this.mContext = context;
    }


    public void addList(List<MyCollectBean.DataBean> mlist){
        if (mlist != null && mlist.size() > 0){
            list.addAll(mlist);
            notifyDataSetChanged();
        }
    }

    public void clearList(){
        list.clear();
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ViewHolder viewHolder = new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.mymoview_adapter_layout,parent,false));
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        Glide.with(mContext)
                .setDefaultRequestOptions(
                        new RequestOptions()
                                .frame(3000000)
                                .centerCrop()
                                .error(R.drawable.dialog_loading)
                                .placeholder(R.drawable.dialog_loading))
                .load(list.get(position).getPic())
                .into(holder.movie_img);


        holder.movie_tv_serialize.setText(list.get(position).getSerialize());
        holder.movie_tv_title.setText(list.get(position).getTitle());

        holder.movie_rel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (TextUtils.isEmpty(list.get(position).getId())){
                    Toast.makeText(mContext,"视频不存在",Toast.LENGTH_LONG).show();
                }else {
                    Intent intent = new Intent(mContext, VideoDetailActivity.class);
                    intent.putExtra("id",list.get(position).getId());
                    mContext.startActivity(intent);
                }

            }
        });

        if (indexVis == 0){
            holder.movie_iv_circle.setVisibility(View.GONE);
        }else {
            holder.movie_iv_circle.setVisibility(View.VISIBLE);
        }

        holder.movie_iv_circle.setChecked(isItemChecked(position));

        holder.movie_iv_circle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isItemChecked(position)) {
                    setItemChecked(position, false);
                } else {
                    setItemChecked(position, true);
                }

                onClickListener.onClick(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        private TextView movie_tv_serialize,movie_tv_title;
        private ImageView movie_img;
        private CheckBox movie_iv_circle;
        private LinearLayout movie_rel;
        public ViewHolder(View itemView) {
            super(itemView);
            movie_tv_serialize = itemView.findViewById(R.id.movie_tv_serialize);
            movie_tv_title = itemView.findViewById(R.id.movie_tv_title);
            movie_img = itemView.findViewById(R.id.movie_img);
            movie_iv_circle  = itemView.findViewById(R.id.movie_iv_circle);
            movie_rel = itemView.findViewById(R.id.movie_rel);
        }
    }

    private void setItemChecked(int position, boolean isChecked) {
        mSelectedPositions.put(position, isChecked);
        notifyDataSetChanged();
    }

    //根据位置判断条目是否选中
    private boolean isItemChecked(int position) {
        return mSelectedPositions.get(position);
    }

    public void setIndexVis(int position){
        this.indexVis = position;
        notifyDataSetChanged();
    }

    //获得选中条目的结果
    public List<MyCollectBean.DataBean> getSelectedItem() {
        List<MyCollectBean.DataBean> selectList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            if (isItemChecked(i)) {
                selectList.add(list.get(i));
            }
        }
        return selectList;
    }
    //全选
    public void setAllSelect() {
        for (int i = 0; i < list.size(); i++) {
            setItemChecked(i,true);
        }
        notifyDataSetChanged();
    }

    //取消全选
    public void closeAllSelect() {
        for (int i = 0; i < list.size(); i++) {
            setItemChecked(i,false);
        }
        notifyDataSetChanged();
    }

    public interface OnClickListener{
        void onClick(int position);
    }

    public void setOnClickListener(OnClickListener onClickListener){
        this.onClickListener = onClickListener;
    }
}
